package com.Blog.www.dao.Impl;

import com.Blog.www.constant.SqlConstant;
import com.Blog.www.dao.BlogDao;
import com.Blog.www.po.BlogImg;
import com.Blog.www.po.UserBlog;
import com.Blog.www.utils.SqlDao;

import java.util.List;

/**
 * 博客增删查改的实现类
 *
 * @author 君未洋
 */
public class BlogDaoImpl implements BlogDao {

    private static SqlDao sqlDao = new SqlDao();

    @Override
    public List<BlogImg> findImgList(String bid) {
        String sql = "select * from blogImg where bid = ?";
        return sqlDao.selectAllBlogImg(sql, bid);
    }

    @Override
    public int findTotalCount(String status) {
        if (status != null) {
            String sql = "select count(*) as count from userblog where status=?";
            return sqlDao.selectCount(sql, status);
        } else {
            String sql = "select count(*) as count from userblog";
            return sqlDao.selectCount(sql);
        }
        //使用这个方法可以直接返回int类型的数据
    }

    @Override
    public int findTotalCount(String columnId, String status) {
        if (status != null) {
            String sql = "SELECT COUNT(*) AS COUNT FROM userblog,blogcolumn " +
                    "WHERE userblog.status= ? AND blogcolumn.columnid= ? AND userblog.id=blogcolumn.blogid;";
            return sqlDao.selectCount(sql, status, columnId);
        } else {
            String sql = "SELECT COUNT(*) AS COUNT FROM userblog,blogcolumn " +
                    "WHERE  blogcolumn.columnid=? AND userblog.id=blogcolumn.blogid;";
            return sqlDao.selectCount(sql, columnId);
        }
    }

    @Override
    public List<UserBlog> findByPage(int start, int rows, String status) {
        if (status != null) {
            String sql = "select * from userblog where status = ? limit ? , ? ";
            return sqlDao.selectAllBlog(sql, status, start, rows);
        } else {
            String sql = "select * from userblog limit ? , ? ";
            return sqlDao.selectAllBlog(sql, start, rows);
        }
    }

    @Override
    public List<UserBlog> findByPage(String columnId, int start, int rows, String status) {
        if (status != null) {
            String sql = "SELECT userblog.id,userblog.uid,userblog.title,userblog.content,userblog.status " +
                    "FROM userblog,blogcolumn " +
                    "WHERE userblog.id=blogcolumn.blogid and blogcolumn.columnid= ? and userblog.status=?" +
                    "LIMIT ?,? ";
            return sqlDao.selectAllBlog(sql, columnId, status, start, rows);
        } else {
            String sql = "SELECT userblog.id,userblog.uid,userblog.title,userblog.content,userblog.status " +
                    "FROM userblog,blogcolumn " +
                    "WHERE userblog.id=blogcolumn.blogid AND blogcolumn.columnid= ? " +
                    "LIMIT ?,? ";
            return sqlDao.selectAllBlog(sql, columnId, start, rows);
        }
    }

    @Override
    public Boolean addBlog(UserBlog blog) {
        //调用sql添加一下
        String sql = "insert into userblog values (null,?,?,?,?)";
        return sqlDao.update(sql, blog.getUid(), blog.getTitle(), blog.getContent(), SqlConstant.STATUS_A);
    }

    @Override
    public int findUserTotalCount(int uid, String status, String labelPrior) {

        if (status == null) {
            if (labelPrior == null || "".equals(labelPrior)) {
                String sql = "select count(*) as count from userblog where uid= ? ";
                return sqlDao.selectCount(sql, uid);
            }
            String sql = "select count(*) as count from userblog as p,blog_label as q  where p.uid = ? and q.label_id = ? and p.id = q.blog_id";
            return sqlDao.selectCount(sql, uid, labelPrior);
        } else {
            String sql = "select count(*) as count  from userblog where status=? and uid=?";
            return sqlDao.selectCount(sql, status, uid);
        }
        //使用这个方法可以直接返回int类型的数据
    }

    @Override
    public List<UserBlog> findUserBlogList(int uid, int start, int rows, String status, String labelPrior) {
        if (status == null) {
            if (labelPrior == null) {
                String sql = "SELECT * from userblog WHERE uid = ? LIMIT ?,? ";
                return sqlDao.selectAllBlog(sql, uid, start, rows);
            }
            String sql = "SELECT  p.id,p.uid,p.title,p.content,p.status  from userblog as p,blog_label as q  WHERE p.uid = ? and q.label_id = ? and p.id = q.blog_id LIMIT ?,? ";
            return sqlDao.selectAllBlog(sql, uid, labelPrior, start, rows);
        } else {
            String sql = "select * from userblog where uid = ? and status = ? LIMIT ?,? ";
            return sqlDao.selectAllBlog(sql, uid, status, start, rows);
        }
    }

    @Override
    public UserBlog findUserBlog(int id, int uid) {
        String sql = null;
        if (uid != 0) {
            sql = "select * from userblog where id = ? and uid = ?";
            return sqlDao.selectBlog(sql, id, uid);
        } else {
            sql = "select * from userblog where id = ?";
            return sqlDao.selectBlog(sql, id);
        }

    }

    @Override
    public UserBlog findUserBlog(String title, String content) {
        String sql = "select * from userblog where title = ? and content = ?";
        return sqlDao.selectBlog(sql, title, content);
    }

    @Override
    public boolean updateBlog(UserBlog blog) {
        String sql = "update userblog set title=?,content=? where id=?";
        return sqlDao.update(sql, blog.getTitle(), blog.getContent(), blog.getId());
    }

    @Override
    public Boolean deleteBlog(int id) {
        String sql = "delete from userblog where id =?";
        return sqlDao.update(sql, id);
    }

    @Override
    public Boolean changeUserBlog(int uid, String status) {
        String sql = "update userblog set status = ?  where uid=?";
        return sqlDao.update(sql, status, uid);
    }

    @Override
    public Boolean changeBlog(String id, String status) {
        String sql = "update userblog set status =?  where id=?";
        return sqlDao.update(sql, status, id);
    }

    @Override
    public int findWordsTotalCount(char[] words) {
        if (words == null || words.length == 0) {
            return 0;
        }
        String sql = "select count(*) as count from userblog where (title like '%";
        for (char word : words) {
            sql += word + "%";
        }
        sql += "' or content like '%";
        for (char word : words) {
            sql += word + "%";
        }
        sql += "' ) and status=?";
        System.out.println(sql);
        return sqlDao.selectCount(sql, SqlConstant.STATUS_B);
    }

    @Override
    public List<UserBlog> findBlogList(char[] words, int start, int rows) {
        if (words == null || words.length == 0) {
            return null;
        }
        String sql = "select * from userblog where (title like '%";
        for (char word : words) {
            sql = sql + word + "%";
        }
        sql += "' or content like '%";
        for (char word : words) {
            sql = sql + word + "%";
        }
        sql += "' ) and status=? limit ?,?";
        System.out.println(sql);
        return sqlDao.selectAllBlog(sql, SqlConstant.STATUS_B, start, rows);
    }

    @Override
    public boolean addImg(String bid, String imgPath) {
        //直接添加即可
        String sql = "insert into blogimg values (null,?,?)";
        return sqlDao.update(sql, bid, imgPath);
    }


}
